#include "stdafx.h"
#include "resource.h"
#include "FtpMgr.h"
#include "maindlg.h"

CMainDlg::CMainDlg() : m_pFtpMgr(NULL)
{
	m_pFtpMgr = new CFtpMgr;

}

CMainDlg::~CMainDlg()
{
	if (m_pFtpMgr)
	{
		delete m_pFtpMgr;
	}
}


unsigned  __stdcall ConnectFtp(LPVOID pParam)
{
	LPCONNECT_PARAM pConnect = (LPCONNECT_PARAM)pParam;
	pConnect->pFtpMgr->ConnectFtp();
	::PostMessage(pConnect->hWnd, WM_CONNECTFTP_FINISH, 0, 0);
	_endthreadex(0); 
	return 0;
}


LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// center the dialog on the screen
	CenterWindow();
	
	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);
	
	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);
	
	UIAddChildWindowContainer(m_hWnd);

	ShowStatus(MAKEINTRESOURCE(IDS_CONNECTING_FTP));
	static CONNECT_PARAM connect_param;
	connect_param.hWnd = m_hWnd;
	connect_param.pFtpMgr = m_pFtpMgr;
	HANDLE handle = (HANDLE)_beginthreadex(NULL,0,ConnectFtp,&connect_param,0,NULL);

	HWND hList = GetDlgItem(IDC_LIST);
	m_list.Attach(hList);
	return TRUE;
}

void CMainDlg::InitList()
{
	m_list.DeleteAllItems();
	int nColumnCount = m_list.GetHeader().GetItemCount();
	for (int i = nColumnCount-1; i >= 0; --i)
	{
		m_list.DeleteColumn(i);
	}

	m_list.SetExtendedListViewStyle(m_list.GetExtendedListViewStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	
	m_list.InsertColumn (0, _T(""), LVCFMT_LEFT, 20);
	m_list.InsertColumn (1, _T("Files"), LVCFMT_LEFT, 300);
	m_list.InsertColumn (2, _T("Progress"), LVCFMT_LEFT, 100);
	
	for (int i = 0; i < m_vFileNameList.size(); ++i)
	{
		m_list.InsertItem(i, _T(""));
		m_list.SetItemText(i, 1, m_vFileNameList[i]);
	}
}

LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add validation code 
	CloseDialog(wID);
	return 0;
}

LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CloseDialog(wID);
	return 0;
}

void CMainDlg::CloseDialog(int nVal)
{
	DestroyWindow();
	::PostQuitMessage(nVal);
}

HRESULT CMainDlg::OnDrag(int u, LPNMHDR pMNHDR, BOOL& )
{
	NMLISTVIEW * pListView = (NMLISTVIEW * )pMNHDR;

	return 0;
}

LRESULT CMainDlg::OnDrop(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
	vector<CString>m_vListTemp;
	HDROP hDrop = (HDROP)wParam;
	TCHAR szFile[MAX_PATH] = {0};
	int nCount = ::DragQueryFile(hDrop, 0xFFFFFFFF, szFile, MAX_PATH);
	if (nCount <= 0)
	{
		return 0;
	}

	for (int i = 0; i < nCount; ++i)
	{
		::ZeroMemory(szFile, sizeof(szFile)/sizeof(TCHAR));
		::DragQueryFile(hDrop, i, szFile, MAX_PATH);
		vector<CString>::iterator it = find(m_vFileNameList.begin(), m_vFileNameList.end(), szFile);
		if (it == m_vFileNameList.end())
		{
			m_vListTemp.push_back(szFile);
		}
	}
	m_vFileNameList.insert(m_vFileNameList.end(),m_vListTemp.begin(), m_vListTemp.end());
	InitList();
	for (int i = 0; i < m_vListTemp.size(); ++i)
	{
		TCHAR * pFile = ::PathFindFileName(m_vListTemp[i]);
		CString strFile = pFile;
		m_pFtpMgr->UploadFile(m_strDefaultPath + _T("/") + strFile,m_vListTemp[i] );
	}


	return 0;
}

void CMainDlg::ShowStatus(const CString & strMessage)
{
	SetDlgItemText(IDC_STATUS, strMessage);
}

LRESULT CMainDlg::OnConnectFtpFinish(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	ShowStatus(MAKEINTRESOURCE(IDS_CONNECTED_FTP));
	m_strServer			= m_pFtpMgr->m_ftpdata.szServer;
	m_strUser			= m_pFtpMgr->m_ftpdata.szUser;
	m_strPwd			= m_pFtpMgr->m_ftpdata.szPwd;
	m_strDefaultPath	= m_pFtpMgr->m_ftpdata.szDefaultPath;
	
	m_vFileNameList.clear();
	m_pFtpMgr->GetFileList(m_strDefaultPath, m_vFileNameList);
	InitList();
	DoDataExchange(FALSE);
	return 0;
}